package planesimulator.game;

import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.action.*;

import planesimulator.plane.*;

/**
 * 
 * Input handler class for class plane.
 * Different KeyInputAction classes change
 * plane class object fields
 * up_down,
 * left_right, 
 * engine_power.
 * @author Jakub Klimek
 * 
 * 
 */
public class PlaneInputHandler extends InputHandler {

  class ActionKeyRudderLeft extends KeyInputAction {

    CessnaCitation pl;
    float coeff;

    ActionKeyRudderLeft(CessnaCitation p, float c) {
      pl = p;
      coeff = c;
    }

    public void performAction(InputActionEvent evt) {
      float t = coeff * evt.getTime();
      pl.rudder_pos += t;


      if (pl.rudder_pos > 30) {
        pl.rudder_pos = 30;
      }
      if (pl.rudder_pos < 0) {
        pl.rudder_pos = 0;
      }
    }
  }

  class ActionKeyRudderRight extends KeyInputAction {

    CessnaCitation pl;
    float coeff;

    ActionKeyRudderRight(CessnaCitation p, float c) {
      pl = p;
      coeff = c;
    }

    public void performAction(InputActionEvent evt) {
      float t = coeff * evt.getTime();
      pl.rudder_pos -= t;
      if (pl.rudder_pos < -30) {
        pl.rudder_pos = -30;
      }
      if (pl.rudder_pos > 0) {
        pl.rudder_pos = 0;
      }
    }
  }

  class ActionKeyUp extends KeyInputAction {

    CessnaCitation pl;
    float coeff;

    ActionKeyUp(CessnaCitation p, float c) {
      pl = p;
      coeff = c;
    }

    public void performAction(InputActionEvent evt) {
      float t = coeff * evt.getTime();
      //if( pl.up_down  < -t )
      pl.up_down += t;
      //else
      //    pl.up_down = 0;

      if (pl.up_down > 30) {
        pl.up_down = 30;
      }
      if (pl.up_down < 0) {
        pl.up_down = 0;
      }
    }
  }

  class ActionKeyDown extends KeyInputAction {

    CessnaCitation pl;
    float coeff;

    ActionKeyDown(CessnaCitation p, float c) {
      pl = p;
      coeff = c;
    }

    public void performAction(InputActionEvent evt) {
      float t = coeff * evt.getTime();
      //if(pl.up_down> t)
      pl.up_down -= t;
      //else
      //    pl.up_down = 0;
      if (pl.up_down < -30) {
        pl.up_down = -30;
      }
      if (pl.up_down > 0) {
        pl.up_down = 0;
      }
    }
  }

  class ActionKeyLeft extends KeyInputAction {

    CessnaCitation pl;
    float coef;

    ActionKeyLeft(CessnaCitation p, float c) {
      coef = c;
      pl = p;
    }

    public void performAction(InputActionEvent evt) {
      float t = coef * evt.getTime();
      if (pl.left_right + t >= 30) {
        pl.left_right = 30;
      } else {
        pl.left_right += t;
      }
      if (pl.left_right < 0) {
        pl.left_right = 0;
      }
    }
  }

  class ActionKeyRight extends KeyInputAction {

    CessnaCitation pl;
    float coef;

    ActionKeyRight(CessnaCitation p, float c) {
      coef = c;
      pl = p;
    }

    public void performAction(InputActionEvent evt) {
      float t = coef * evt.getTime();
      if (pl.left_right - t <= -30) {
        pl.left_right = -30;
      } else {
        pl.left_right -= t;
      }
      if (pl.left_right > 0) {
        pl.left_right = 0;
      }
    }
  }

  class ActionEnginePowerSet extends KeyInputAction {

    CessnaCitation pl;
    float ep;

    ActionEnginePowerSet(CessnaCitation p, float ep) {
      pl = p;
      this.ep = ep;
    }

    public void performAction(InputActionEvent evt) {
      pl.engine_power = ep;
    }
  }

  public PlaneInputHandler(CessnaCitation node, String api) {
    setKeyBindings(api);
    setActions(node);
  }

  private void setKeyBindings(String api) {
    KeyBindingManager keyboard = KeyBindingManager.getKeyBindingManager();
    keyboard.set("up", KeyInput.KEY_UP);
    keyboard.set("down", KeyInput.KEY_DOWN);
    keyboard.set("rleft", KeyInput.KEY_A);
    keyboard.set("rright", KeyInput.KEY_S);

    keyboard.set("left", KeyInput.KEY_LEFT);
    keyboard.set("right", KeyInput.KEY_RIGHT);
    keyboard.set("e0", KeyInput.KEY_GRAVE);
    keyboard.set("e1", KeyInput.KEY_1);
    keyboard.set("e2", KeyInput.KEY_2);
    keyboard.set("e3", KeyInput.KEY_3);
    keyboard.set("e4", KeyInput.KEY_4);
    keyboard.set("e5", KeyInput.KEY_5);
    keyboard.set("e6", KeyInput.KEY_6);
    keyboard.set("e7", KeyInput.KEY_7);
    keyboard.set("e8", KeyInput.KEY_8);
    keyboard.set("e9", KeyInput.KEY_9);
    keyboard.set("e10", KeyInput.KEY_0);

  }

  private void setActions(CessnaCitation node) {
    ActionKeyUp up = new ActionKeyUp(node, 3.5f);
    ActionKeyDown down = new ActionKeyDown(node, 2.5f);


    ActionKeyLeft left = new ActionKeyLeft(node, 1.75f);
    ActionKeyRight right = new ActionKeyRight(node, 1.75f);
    ActionKeyRudderLeft rudder_left = new ActionKeyRudderLeft(node, 3f);
    ActionKeyRudderRight rudder_right = new ActionKeyRudderRight(node, 3f);

    addAction(rudder_left, "rleft", true);
    addAction(rudder_right, "rright", true);
    addAction(up, "up", true);
    addAction(down, "down", true);
    addAction(left, "left", true);
    addAction(right, "right", true);

    ActionEnginePowerSet e0 = new ActionEnginePowerSet(node, 0.0f);
    ActionEnginePowerSet e1 = new ActionEnginePowerSet(node, 0.1f);
    ActionEnginePowerSet e2 = new ActionEnginePowerSet(node, 0.2f);
    ActionEnginePowerSet e3 = new ActionEnginePowerSet(node, 0.3f);
    ActionEnginePowerSet e4 = new ActionEnginePowerSet(node, 0.4f);
    ActionEnginePowerSet e5 = new ActionEnginePowerSet(node, 0.5f);
    ActionEnginePowerSet e6 = new ActionEnginePowerSet(node, 0.6f);
    ActionEnginePowerSet e7 = new ActionEnginePowerSet(node, 0.7f);
    ActionEnginePowerSet e8 = new ActionEnginePowerSet(node, 0.8f);
    ActionEnginePowerSet e9 = new ActionEnginePowerSet(node, 0.9f);
    ActionEnginePowerSet e10 = new ActionEnginePowerSet(node, 1f);

    addAction(e0, "e0", true);
    addAction(e1, "e1", true);
    addAction(e2, "e2", true);
    addAction(e3, "e3", true);
    addAction(e4, "e4", true);
    addAction(e5, "e5", true);
    addAction(e6, "e6", true);
    addAction(e7, "e7", true);
    addAction(e8, "e8", true);
    addAction(e9, "e9", true);
    addAction(e10, "e10", true);

  }
}
